package light.mvc.controller.record;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.model.workflow.Tfield;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.RoomEvent;
import light.mvc.pageModel.meetingroom.MeetingroomEvent;
import light.mvc.pageModel.record.RoomSchedule;
import light.mvc.service.record.RoomScheduleServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/roomSchedule" })
public class RoomScheduleController extends BaseController {
	@Autowired
	private RoomScheduleServiceI roomScheduleService;

	@ResponseBody
	@RequestMapping({ "/addRoomEvent" })
	public Json addRoomEvent(HttpServletRequest request) {
		Json json = new Json();
		try {
			RoomEvent event = new RoomEvent();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// Date start = new Date(Long.valueOf(request.getParameter("start")));
			Date start = null;
			Date end = null;
			start = dateFormat.parse(request.getParameter("start"));
			end = dateFormat.parse(request.getParameter("end"));
			if(start.after(end) || start.equals(end)){
				json.setSuccess(false);
				return json; 
			}
			event.setRoomid(Integer.valueOf(request.getParameter("roomId")));
			event.setStart(start);
			event.setEnd(end);
			event.setUserId(Integer.valueOf(request.getParameter("userId")));
			event.setName(request.getParameter("title"));
			json.setSuccess(this.roomScheduleService.orderRoomEvent(event));
			json.setMsg("与其他会议冲突！");
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			return json;
		}
	}

	@ResponseBody
	@RequestMapping({ "/dataGrid" })
	public Json dataGrid(HttpServletRequest request) {
		Json json = new Json();
		try {
			RoomSchedule r = new RoomSchedule();
			Long id = Long.valueOf(request.getParameter("roomid"));
			if (id != 0) {
				r.setRoomid(id.intValue());
			} else {
				r.setRoomid(1);
			}
			json.setObj(this.roomScheduleService.getDurationEvent(r));
			json.setSuccess(true);
			return json;
			
		} catch (Exception e) {
			json.setSuccess(false);
			return json;
		}
	}

	@ResponseBody
	@RequestMapping({ "/getEventById" })
	public Json getEventById(HttpServletRequest request) {
		Json json = new Json();
		try {
			MeetingroomEvent r = new MeetingroomEvent();
			Long id = Long.valueOf(request.getParameter("eventId"));
			r.setId(id.intValue());
			json.setObj(this.roomScheduleService.getEventById(r));
			json.setSuccess(true);
			return json;
			
		} catch (Exception e) {
			json.setSuccess(false);
			return json;
		}
	}

	@ResponseBody
	@RequestMapping({ "/updateRoomEvent" })
	public Json updateRoomEvent(HttpServletRequest request) {
		Json json = new Json();
		RoomSchedule roomSchedule = new RoomSchedule();
		Date start = new Date(Long.valueOf(request.getParameter("start")));
		Date end = new Date(Long.valueOf(request.getParameter("end")));
		roomSchedule.setStart(start);
		roomSchedule.setId(Long.valueOf(request.getParameter("id")));
		roomSchedule.setEnd(end);
		String ownerId = request.getParameter("ownerId");
		if (ownerId != null && !ownerId.equals("")) {
			roomSchedule.setUserid(Integer.valueOf(ownerId));
		}
		String loginId = request.getParameter("loginId");
		if (loginId != null && !loginId.equals("")) {
			roomSchedule.setUserid(Integer.valueOf(loginId));
		}
		if (this.roomScheduleService.updateRoomEvent(roomSchedule)) {
			json.setSuccess(true);
			json.setMsg("更新成功！");
		} else {
			json.setSuccess(false);
			json.setMsg("更新失败！");
		}
		return json;
	}

	@ResponseBody
	@RequestMapping({ "/updateRoomEventNew" })
	public Json updateRoomEventNew(HttpServletRequest request) {
		Json json = new Json();
		RoomSchedule roomSchedule = new RoomSchedule();
		roomSchedule.setId(Long.valueOf(request.getParameter("id")));
		roomSchedule.setOwner(request.getParameter("owner"));
		roomSchedule.setRoomName(request.getParameter("roomName"));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = null;
		Date end = null;
		try {
			start = dateFormat.parse(request.getParameter("start"));
			end = dateFormat.parse(request.getParameter("end"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		roomSchedule.setStart(start);
		roomSchedule.setEnd(end);
		roomSchedule.setName(request.getParameter("title"));
		roomSchedule.setUserid(Integer.valueOf(request.getParameter("ownerId")));
		json.setSuccess(this.roomScheduleService.updateRoomEvent(roomSchedule));
		return json;
	}

	@ResponseBody
	@RequestMapping({ "/deleteRoomEvent" })
	public Json deleteRoomEvent(HttpServletRequest request) {
		Json json = new Json();
		RoomEvent event = new RoomEvent();
		event.setId(Long.valueOf(request.getParameter("id")));
		json.setSuccess(this.roomScheduleService.deleteRoomEvent(event));
		return json;
	}

	@RequestMapping({ "/addThemeUI" })
	public String addThemeUI(HttpServletRequest req, @RequestParam("roomId") String roomId, @RequestParam("userId") String userId, @RequestParam("startTimeForAddTheme") String startTimeForAddTheme, @RequestParam("endTimeForAddTheme") String endTimeForAddTheme) {
		Date endDate = new Date(Long.valueOf(endTimeForAddTheme));
		Date startDate = new Date(Long.valueOf(startTimeForAddTheme));
		/*
		 * Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); calendar.add(Calendar.DAY_OF_MONTH, 1); Date newStartDate = calendar.getTime();
		 * 
		 * Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(endDate); calendar2.add(Calendar.DAY_OF_MONTH, 1); Date newEndDate = calendar2.getTime();
		 */
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:ss");

		req.setAttribute("startTimeForAddTheme", dateFormat.format(startDate));
		req.setAttribute("endTimeForAddTheme", dateFormat.format(endDate));
		req.setAttribute("roomId", roomId);
		req.setAttribute("userId", userId);

		return "/meetingroom/addThemeUI";
	}

}
